package com.fc.mall.service.impl;

import com.fc.mall.dao.UmsAdminDao;
import com.fc.mall.dao.UmsAdminPermissionRelationDao;
import com.fc.mall.dao.UmsAdminRoleRelationDao;
import com.fc.mall.dto.UmsAdminDto;
import com.fc.mall.dto.UmsAdminParam;
import com.fc.mall.dto.UmsPermissionNode;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.*;
import com.fc.mall.model.*;
import com.fc.mall.mapper.*;
import com.fc.mall.model.*;
import com.fc.mall.service.UmsAdminService;
import com.fc.mall.util.JwtTokenUtil;
import com.github.pagehelper.PageHelper;
import ma.glasnost.orika.MapperFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * UmsAdminService实现类
 * Created by dobuy on 2018/4/26.
 */
@Service
public class UmsAdminServiceImpl implements UmsAdminService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminServiceImpl.class);
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private UmsAdminMapper adminMapper;
    @Autowired
    private UmsAdminRoleRelationMapper adminRoleRelationMapper;
    @Autowired
    private UmsAdminRoleRelationDao adminRoleRelationDao;
    @Autowired
    private UmsAdminPermissionRelationMapper adminPermissionRelationMapper;
    @Autowired
    private UmsAdminPermissionRelationDao adminPermissionRelationDao;
    @Autowired
    private UmsAdminLoginLogMapper loginLogMapper;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private UmsAdminDao umsAdminDao;

    @Autowired
    private UmsPermissionMapper permissionMapper;


    @Override
    public UmsAdmin getAdminByUsername(String username) {
        UmsAdminExample example = new UmsAdminExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<UmsAdmin> adminList = adminMapper.selectByExample(example);
        if (adminList != null && adminList.size() > 0) {
            return adminList.get(0);
        }
        return null;
    }

    @Override
    public UmsAdmin register(UmsAdminParam umsAdminParam) {
        UmsAdmin umsAdmin = new UmsAdmin();
        BeanUtils.copyProperties(umsAdminParam, umsAdmin);
        umsAdmin.setCreateTime(new Date());
        umsAdmin.setStatus(1);
        //查询是否有相同用户名的用户
        UmsAdminExample example = new UmsAdminExample();
        example.createCriteria().andUsernameEqualTo(umsAdmin.getUsername());
        List<UmsAdmin> umsAdminList = adminMapper.selectByExample(example);
        if (umsAdminList.size() > 0) {
            throw new ServiceException("该用户已经存在！");
        }
        //将密码进行加密操作
        String md5Password = passwordEncoder.encode(umsAdmin.getPassword());
        umsAdmin.setPassword(md5Password);
        adminMapper.insert(umsAdmin);
        updateRole(umsAdmin.getId(), umsAdmin.getRoleIdList());
        return umsAdmin;
    }

    @Override
    public String login(String username, String password) {
        String token = null;
        //密码需要客户端加密后传递
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
        try {
            Authentication authentication = authenticationManager.authenticate(authenticationToken);
            SecurityContextHolder.getContext().setAuthentication(authentication);
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            token = jwtTokenUtil.generateToken(userDetails);
            updateLoginTimeByUsername(username);
            insertLoginLog(username);
        } catch (AuthenticationException e) {
            LOGGER.warn("登录异常:{}", e.getMessage());
        }
        return token;
    }

    /**
     * 添加登录记录
     * @param username 用户名
     */
    private void insertLoginLog(String username) {
        UmsAdmin admin = getAdminByUsername(username);
        UmsAdminLoginLog loginLog = new UmsAdminLoginLog();
        loginLog.setAdminId(admin.getId());
        loginLog.setCreateTime(new Date());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        loginLog.setIp(request.getRemoteAddr());
        loginLogMapper.insert(loginLog);
    }

    /**
     * 根据用户名修改登录时间
     */
    private void updateLoginTimeByUsername(String username) {
        UmsAdmin record = new UmsAdmin();
        record.setLoginTime(new Date());
        UmsAdminExample example = new UmsAdminExample();
        example.createCriteria().andUsernameEqualTo(username);
        adminMapper.updateByExampleSelective(record, example);
    }

    @Override
    public String refreshToken(String oldToken) {
        String token = oldToken.substring(tokenHead.length());
        if (jwtTokenUtil.canRefresh(token)) {
            return jwtTokenUtil.refreshToken(token);
        }
        return null;
    }

    @Override
    public UmsAdmin getItem(Long id) {
        UmsAdmin umsAdmin = adminMapper.selectByPrimaryKey(id);
        umsAdmin.setPassword("");
        umsAdmin.setComfirmPassword("");
        List<UmsRole> roleList = adminRoleRelationDao.getRoleList(id);
        List<Long> roleIds = new ArrayList<>();
        for (UmsRole role : roleList)
        {
            roleIds.add(role.getId());
        }
        umsAdmin.setRoleIdList(roleIds);
        return umsAdmin;
    }

    @Override
    public List<UmsAdminDto> list(String name, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        UmsAdminParam param = new UmsAdminParam();
        param.setUsername(name);
        return umsAdminDao.list(param);
    }

    @Override
    public int update(Long id, UmsAdmin admin) {
        admin.setId(id);
        updateRole(id, admin.getRoleIdList());
        if(StringUtils.isEmpty(admin.getPassword()))
        {
            admin.setPassword(null);
        }
        else
        {
            String md5Password = passwordEncoder.encode(admin.getPassword());
            admin.setPassword(md5Password);
        }
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public int delete(Long id) {
        UmsAdmin admin = adminMapper.selectByPrimaryKey(id);
        //当用户名为admin时不可删除
        String username = "admin";
        if (StringUtils.isEmpty(admin) || username.equals(admin.getUsername())){
            throw new ServiceException("超级管理员不可删除！");
        }

        //删除permissionRelation中该用户信息
        UmsAdminPermissionRelationExample permissionRelationExample = new UmsAdminPermissionRelationExample();
        permissionRelationExample.createCriteria().andAdminIdEqualTo(id);
        adminPermissionRelationMapper.deleteByExample(permissionRelationExample);

        //删除roleRelation中该用户信息
        UmsAdminRoleRelationExample roleRelationExample = new UmsAdminRoleRelationExample();
        roleRelationExample.createCriteria().andAdminIdEqualTo(id);
        adminRoleRelationMapper.deleteByExample(roleRelationExample);

        return adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateRole(Long adminId, List<Long> roleIds) {
        int count = roleIds == null ? 0 : roleIds.size();
        //先删除原来的关系
        UmsAdminRoleRelationExample adminRoleRelationExample = new UmsAdminRoleRelationExample();
        adminRoleRelationExample.createCriteria().andAdminIdEqualTo(adminId);
        adminRoleRelationMapper.deleteByExample(adminRoleRelationExample);
        //建立新关系
        if (!CollectionUtils.isEmpty(roleIds)) {
            List<UmsAdminRoleRelation> list = new ArrayList<>();
            for (Long roleId : roleIds) {
                UmsAdminRoleRelation roleRelation = new UmsAdminRoleRelation();
                roleRelation.setAdminId(adminId);
                roleRelation.setRoleId(roleId);
                list.add(roleRelation);
            }
            adminRoleRelationDao.insertList(list);
        }
        return count;
    }

    @Override
    public List<UmsRole> getRoleList(Long adminId) {
        return adminRoleRelationDao.getRoleList(adminId);
    }

    @Override
    public int updatePermission(Long adminId, List<Long> permissionIds) {
        //删除原所有权限关系
        UmsAdminPermissionRelationExample relationExample = new UmsAdminPermissionRelationExample();
        relationExample.createCriteria().andAdminIdEqualTo(adminId);
        adminPermissionRelationMapper.deleteByExample(relationExample);
        //获取用户所有角色权限
        List<UmsPermission> permissionList = adminRoleRelationDao.getRolePermissionList(adminId);
        List<Long> rolePermissionList = permissionList.stream().map(UmsPermission::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(permissionIds)) {
            List<UmsAdminPermissionRelation> relationList = new ArrayList<>();
            //筛选出+权限
            List<Long> addPermissionIdList = permissionIds.stream().filter(permissionId -> !rolePermissionList.contains(permissionId)).collect(Collectors.toList());
            //筛选出-权限
            List<Long> subPermissionIdList = rolePermissionList.stream().filter(permissionId -> !permissionIds.contains(permissionId)).collect(Collectors.toList());
            //插入+-权限关系
            relationList.addAll(convert(adminId,1,addPermissionIdList));
            relationList.addAll(convert(adminId,-1,subPermissionIdList));
            return adminPermissionRelationDao.insertList(relationList);
        }
        return 0;
    }

    /**
     * 将+-权限关系转化为对象
     */
    private List<UmsAdminPermissionRelation> convert(Long adminId,Integer type,List<Long> permissionIdList) {
        List<UmsAdminPermissionRelation> relationList = permissionIdList.stream().map(permissionId -> {
            UmsAdminPermissionRelation relation = new UmsAdminPermissionRelation();
            relation.setAdminId(adminId);
            relation.setType(type);
            relation.setPermissionId(permissionId);
            return relation;
        }).collect(Collectors.toList());
        return relationList;
    }

    @Override
    public List<UmsPermission> getPermissionList(Long adminId) {
        return adminRoleRelationDao.getPermissionList(adminId);
    }

    @Override
    public List<UmsPermissionNode> getPermissionTreeListByUsername(String username) {
        UmsAdmin umsAdmin = getAdminByUsername(username);
        if(null == umsAdmin)
        {
            throw new ServiceException("当前用户不存在");
        }
        List<UmsPermission> permissionList = getPermissionList(umsAdmin.getId());

        //查询出该用户所拥有菜单的父级菜单
        List<Long> pidList = new ArrayList<>();
        permissionList.forEach(umsPermission -> {
            pidList.add(umsPermission.getPid());
        });
        UmsPermissionExample example = new UmsPermissionExample();
        example.createCriteria().andIdIn(pidList);
        List<UmsPermission> parentPermissionList = permissionMapper.selectByExample(example);
        //去重添加
        permissionList.removeAll(parentPermissionList);
        permissionList.addAll(parentPermissionList);

        List<UmsPermissionNode> umsPermissionNodeList = permissionList.stream()
                .filter(permission -> permission.getPid().equals(0L))
                .sorted((p1 ,p2) -> p1.getSort().compareTo(p2.getSort()))
                .map(permission -> UmsPermissionServiceImpl.covert(permission,permissionList)).collect(Collectors.toList());
        return umsPermissionNodeList;
    }
}
